/**
 * 
 */
package com.zznode.client.wlanpolling.model;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @author Bean
 * 
 */
public class DatabaseHelper extends SQLiteOpenHelper {
	public static final String APMAC = "apMac";
	public static final int DATABASE_VERSION = 5;
	public static final String DATABASE_NAME = "polltest.db";
	public static final String TABLE_TESTRESULT = "tb_testresult";
	public static final String TABLE_WIRELESSENVAPINFO = "tb_wirelessenvapinfo";
	public static final String TABLE_WEBSITE = "tb_website";
	public static final String TABLE_WEBSITERESULT = "tb_websiteresult";
	
	public static final String CREATETABLE_TESTRESULT_SQL = "CREATE TABLE tb_testresult (id INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ " uid text, sysApMac text, apMac text, hotname text, hotcode text, hottype text, hotsubtype text, hotlat text, hotlon text, "
			+ " landmark text, ipregion text, worktime text, hotaddress text, apCodeNo text, apIp text, apDevName text, apLevel text, apSerNo text,"
			+ " apWorkTime text, apCoverTech text, apTranType text, apContactPerson text, apContactFun text, apCoverArea text, ipAddress text, "
			+ " averageRate text, peekRate text, apnettimelag text, apnetloss text, netskytimelag text, netskyloss text, rssival1 text, rssival2 text, rssival3 text, rssival4 text, "
			+ " snrval1 text, snrval2 text, snrval3 text, snrval4 text, delayval1 text, delayval2 text, delayval3 text, delayval4 text, packlostval1 text, "
			+ " packlostval2 text, packlostval3 text, packlostval4 text, rssiremark1 text, rssiremark2 text, rssiremark3 text, rssiremark4 text, devAp1 text, "
			+ " devAp2 text, changetimelag text, changeloss text, url text, apNetIp text, netskyIp text, netskyNet text, bandwidth text, username text, remark text, "
			+ " areaid text, citycode text, uploadSpeed text, uploadDatasize text, downloadSpeed text, downloadDatasize text, manualPingDelay text,"
			+ " manualPingLossPackage text, manualPingParams text, relTestCount text, relTestDelay text, apLat text, apLon text, testtime datetime, " 
			+ " apvlan text, apver text, aploc text, apsec1 text, apsec2 text, apSwitchName text, apSwitchPortName text, apSwitchBandwidth text, hotspotTel text," 
			+ " phonemac text, phonecompany text, phonetype text, apRemark text, ssid text)";

	public static final String CREATETABLE_WIRELESSENVAPINFO_SQL = "CREATE TABLE tb_wirelessenvapinfo(id INTEGER PRIMARY KEY AUTOINCREMENT,  "
			+ " curApMac text, nearmac text, nearssidname text, nearChannel text, nearrssi text, nearfrquency text, nearprotocol text,"
			+ " neartype text)";
	
	public static final String CREATETABLE_WEBSITE = "CREATE TABLE tb_website(id INTEGER PRIMARY KEY AUTOINCREMENT," +
			" name text, url text)";
	
	public static final String CREATETABLE_WEBSITERESULT = "CREATE TABLE tb_websiteresult(id INTEGER PRIMARY KEY AUTOINCREMENT ," +
			" name text, url text, speed text, avgSpeed text, apMac text)";
	
	public static final String DROPTABLE_TESTRESULT_SQL = "drop table tb_testresult";
	public static final String DROPTABLE_WIRELESSENVAPINFO_SQL = "drop table tb_wirelessenvapinfo";
	public static final String DROPTABLE_WEBSITE_SQL = "drop table tb_website";
	public static final String DROPTABLE_WEBSITERESULT_SQL = "drop table tb_websiteresult";
	
	DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		try {
			db.execSQL(CREATETABLE_TESTRESULT_SQL);
			db.execSQL(CREATETABLE_WIRELESSENVAPINFO_SQL);
			db.execSQL(CREATETABLE_WEBSITE);
			db.execSQL(CREATETABLE_WEBSITERESULT);
		} catch (Exception e) {
			Log.d("Bru_Press_Data", "exception in table created");
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		try {
			db.execSQL(DROPTABLE_TESTRESULT_SQL);
			db.execSQL(DROPTABLE_WIRELESSENVAPINFO_SQL);
			db.execSQL(DROPTABLE_WEBSITE_SQL);
			db.execSQL(DROPTABLE_WEBSITERESULT_SQL);
			
			db.execSQL(CREATETABLE_TESTRESULT_SQL);
			db.execSQL(CREATETABLE_WIRELESSENVAPINFO_SQL);
			db.execSQL(CREATETABLE_WEBSITE);
			db.execSQL(CREATETABLE_WEBSITERESULT);
		} catch (Exception e) {
			Log.d("Bru_Press_Data", "exception in table created");
		}
	}
}
